package com.alan.base.adapter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class AdapterFactory {

    public static <T> SimpleAdapter<T> createSimpleAdapter(@NonNull Context context, @Nullable List<T> list, @NonNull SimpleView<T> view) {
        return new SimpleAdapter<>(context, view, list);
    }

    public static <VT extends View, VI extends View, DT, DI> TitleListAdapter<VT, VI, DT, DI> createTitleAdapter(
            @NonNull Context context,
            @NonNull TitleView<VT, VI, DT, DI> titleView,
            List<Object> list,
            Class<DT> dtClass) {
        return new TitleListAdapter<>(context, titleView, list,dtClass);
    }

    public static <VT extends View, VI extends View, DT, DI> TitleListAdapter<VT, VI, DT, DI> createTitleAdapter(
            @NonNull Context context,
            @NonNull TitleView<VT, VI, DT, DI> titleView,
            List<Object> list,
            Class<DT> dtClass,
            RecyclerView recyclerView,
            int spanCount) {
        TitleListAdapter<VT, VI, DT, DI> adapter = new TitleListAdapter<>(context, titleView, list, dtClass);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,spanCount);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemViewType = adapter.getItemViewType(position);
                return itemViewType == TitleListAdapter.VIEW_TYPE_TITLE ? spanCount : 1;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        return adapter;
    }
}
